﻿using System;
using System.Web.UI;
using SARS.Biz;
using SARS.Entity;
using SARS.Entity.Factory;

namespace SARS.Student
{
    public partial class AbsenceNotify : Page
    {
        private readonly StudentAbsentReasonBO absentBO = new StudentAbsentReasonBO();
        private readonly CourseBO courseBiz = new CourseBO();
        private readonly ProgramBO programBiz = new ProgramBO();
        private readonly SessionBO sessionBiz = new SessionBO();
        private readonly UserBO userBiz = new UserBO();
        private StudentAbsenceReasonEntity absenceReason;
        private string attendenceId = string.Empty;
        private CourseEntity course;
        private ProgramEntity program;
        private SessionEntity session;
        private StudentEntity student;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            string sessionId = Request.QueryString["sesid"];
            attendenceId = Request.QueryString["atid"];
            string studentId = Request.QueryString["stid"];
            string courseId = Request.QueryString["cid"];
            string programId = Request.QueryString["prid"];

            if (!string.IsNullOrEmpty(sessionId)
                && !string.IsNullOrEmpty(studentId)
                && !string.IsNullOrEmpty(courseId)
                && !string.IsNullOrEmpty(programId))
            {
                session = sessionBiz.GetSessionById(Int32.Parse(sessionId));
                course = courseBiz.RetrieveCourseById(Int32.Parse(courseId));
                program = programBiz.RetrieveProgramById(Int32.Parse(programId));
                student = (StudentEntity) userBiz.RetrieveUserById(Int32.Parse(studentId));
                absenceReason = absentBO.GetStudentAbsenceReasonById(Int32.Parse(attendenceId));

                if (!IsPostBack)
                {
                    if (absenceReason.AttendanceSessionId == Int32.Parse(attendenceId))
                    {
                        txtReason.Text = absenceReason.StudentAbsenceReason;
                        lblStatus.Text = absenceReason.Approved;
                        UserEntity approver = userBiz.RetrieveUserById(absenceReason.ModifiedBy);
                        if (approver != null)
                        {
                            lblApprover.Text = approver.FullName;
                        }
                    }

                    lblProgram.Text = program.ProgramName;
                    lblCourse.Text = course.CourseInfo;
                    lblSession.Text = session.SessionTopic;
                    lblRoom.Text = session.Room.RoomName;
                    lblTutor.Text = session.Manager.FullName;
                    lblStart.Text = session.StartDate.ToString();
                }
            }
        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("ViewAttendanceRecord.aspx");
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            StudentAbsenceReasonEntity absence = StudentAbsenceReasonFactory.Create();
            absence.AttendanceSessionId = Int32.Parse(attendenceId);
            absence.CreatedDate = DateTime.Now;
            absence.Creator = student.UserID;
            absence.ModifiedBy = student.UserID;
            absence.StudentAbsenceReason = txtReason.Text;

            StudentAbsenceReasonEntity ab = absentBO.GetStudentAbsenceReasonById(Int32.Parse(attendenceId));
            if (ab.AttendanceSessionId == Int32.Parse(attendenceId))
            {
                if (ab.IsApproved == 1 || ab.IsApproved == 2)
                {
                    txtReason.Text = ab.StudentAbsenceReason;
                    Response.Write("<script>alert('You cannot update this absence request notify!')</script");
                    return;
                }
                absentBO.UpdateStudentAbsenceReason(absence);
            }
            else
            {
                absentBO.InsertStudentAbsenceReason(absence);
                txtReason.Text = absence.StudentAbsenceReason;
                lblStatus.Text = absence.Approved;
                Response.Write("<script>alert('Your absence request successful!')</script");
            }
        }
    }
}